
/**
 * Created on 2016/9/21
 */

$(function ()
{
    var baseUrl = "http://192.168.2.205:9999/ebjcfsweb/"; 
    var detailsEle = document.querySelector(".coffee-select-details");
    var $downloadBtns = $('.location-download-coffee');
    var $browserTips = $("#browserTips");
    var coffeeName = document.querySelector(".coffee-select-title");
    var readsCnt = document.querySelector(".coffee-select-number");
    var errorBox = document.querySelector("#error-box");
 
    coffeeName.innerText = window.sessionStorage.getItem("name");
    readsCnt.innerText = window.sessionStorage.getItem("readCnt");
    detailsEle.innerText = window.sessionStorage.getItem("about");
    
    // 生成参数，签名
   var lessName = GetQueryString("lessId");
   var lessId = '{"lessId":"'+lessName+'"}';
   var nowsDate = new Date();
   var sendsTime = nowsDate.getTime();;
   var signPosLess = Crypto.MD5("sendTime="+sendsTime+'data={"lessId":"'+lessName+'"}').toUpperCase();
   
   // 获得推荐课程
   $.ajax({
       url: baseUrl + 'getPosLessList',
       type: 'POST',
       dataType: 'json',
       data: {
          sign : signPosLess,
          signType : '01',
          data : lessId,
          authToken : "QYVD8W2JJW4KYDP2YMKWFX36",
          sendTime : sendsTime,
          appType : '01',
          version : '1.0'
       },
       success:function(res){
          var data = res.data;
          var result = JSON.parse(data);
          if (res.rspCode == "0000") {
             // 视频获取失败
             if (window.sessionStorage.getItem("dataflag") == 1) {
                $('header, article, section').css('display','none');
                errorBox.style.display = 'block';
                return false;
             }
             // 推荐课程不为空
             if(result.list.length > 0){
                var imgsrc = baseUrl + "path/";
                for(var i=0; i< result.list.length; i++){             
                    var content = '<div class="made-details-title">' +
                                   '<p class="less-name">'+result.list[i].name+'</p>'+
                                   '<img src="'+imgsrc+result.list[i].imgPath+'">'+
                               '</div>';
                    $("section").append(content);
                }
             //推荐课程为空
             } else {
               var noList = document.querySelector(".less-no-list");
               noList.classList.remove('none');
             }
          } else {
            $('header, article, section').css('display','none');
            errorBox.style.display = 'block';
          }
       },
       error:function(){
          alert("服务器繁忙，请稍后再试");
       }
   });

    // 文字详情
    function detailsClass(target,obj){
        if (target.classList.contains(obj)){
            target.classList.remove(obj);
        } else {
            target.classList.add(obj);
        }
    };

    detailsEle.addEventListener("click", function (event)
    {
        event.preventDefault();      
        event.stopPropagation();
        var target = event.target;
        detailsClass(target,'text-overflows');
    }, false);

    // 微信下载
    $downloadBtns.on("touchstart", function (e)
    {
        e.preventDefault();
        e.stopPropagation();
        var ua = navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger")
        {
            $browserTips.show();
        }
        $browserTips.find(".browser-tips-mask").on("touchstart", function (event)
        {
            $browserTips.hide();
            event.preventDefault();      
            event.stopPropagation();
        }
        );
    }
    );

    //返回顶部显示与隐藏
    // window.onscroll = function ()
    // {
    //     var scroll = document.body.scrollTop || document.documentElement.scrollTop,
    //         gotop = document.querySelector(".go-top-box");

    //     if (scroll > $(window).height())
    //     {
    //         gotop.classList.remove("none");
    //     }
    //     else
    //     {
    //         gotop.classList.add("none");
    //     }
    // }

    // // 返回顶部
    // $("#goTop").bind("touchstart", function ()
    // {
    //     $("html, body").animate(
    //     {
    //         scrollTop : 0
    //     }, 300);
    // }
    // );
    
});

//获得相关参数
function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

